$(function() {
    hideWaitMask();
    $("#new-dir-dialog").dialog({
        autoOpen : false,
        height : 200,
        width : 300,
        modal : true,
        buttons : {
            "确定" : function() {
                var cwd = $("#cwd").val(), newdirname = $.trim($('#newdirname').val());
                $(this).dialog("close");
                if (newdirname) {
                    showWaitMask('正在创建目录...');
                    $.ajax({
                        url : './?Ctlr=NewDir',
                        data : {
                            pwd : cwd,
                            dname : newdirname
                        },
                        transit:function(transit,url){
                            hideWaitMask();
                            transitDialog(transit,url,createDirCallback);
                        },
                        success : createDirCallback
                    });
                }
            },
            "取消" : function() {
                $(this).dialog("close");
            }
        },
        close : function() {
            $('#newdirname').val('');
        }
    });
    $('#btn-new-dir').click(function() {
        $("#new-dir-dialog").dialog('open');
        return false;
    });
    $('#upload-from').submit(function(){
        var me = $(this);
        showWaitMask('正在处理...');
        me.ajaxSubmit({
            transit:function(transit,url){                
                transitDialog(transit,url,uploadedCallback);
                hideWaitMask();
            },
            success :uploadedCallback
        });
        return false;
    });
    $("#upload-file-dialog").dialog({
        autoOpen : false,        
        width : 750,
        height: 425,
        modal : true,
        buttons : {
            "确定" : function() {
                var uploader = $('#uploader').pluploadQueue();  
                if (uploader.files.length > 0 && uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) {
                    $(this).dialog("close");                
                    $("#upload-cwd").val($('#cwd').val());
                    $('#upload-from').submit();
                }else if(uploader.files.length > 0){
                    alert('队列中尚有文件未上传,请先上传或删除.');
                }else{
                    $(this).dialog("close");
                }
            },
            "取消" : function() {
                $(this).dialog("close");
            }
        },
        close : function() {
            $("#upload-cwd").val('');
        }
    });
    
    $('#btn-upload').click(function() {
        $("#upload-file-dialog").dialog('open');
        return false;
    });
    // Spanish
    plupload.addI18n({
        'Select files' : '文件上传',
        'Add files to the upload queue and click the start button.' : '请点击“添加文件”按钮选择文件或将文件拖到队列.然后点击“上传”按钮。',
        'Filename' : '文件名',
        'Status' : '进度',
        'Size' : '大小',
        'Add files' : '添加文件',
        'Stop current upload' : '停止上传',
        'Start uploading queue' : '上传',
        'Uploaded %d/%d files': '已经上传 %d/%d 个文件',
        'N/A' : 'N/A',
        'Drag files here.' : '直接将文件拖动到这里',
        'File extension error.': '文件扩展名错误.',
        'File size error.': '文件尺寸错误.',
        'Init error.': '初始化失败.',
        'HTTP Error.': 'HTTP错误.',
        'Security error.': '安全错误.',
        'Generic error.': '一般错误.',
        'IO error.': 'IO错误.',
        'Stop Upload': '停止上传.',
        'Add Files': '添加文件',
        'Start upload': '上传',
        '%d files queued': '%d 个文件已排队'
    });
    
    $("#uploader").pluploadQueue({      
        runtimes : 'flash,html4',       
        url : '../share/?Ctlr=PlUpload',
        max_file_size : '10mb',
        max_file_count: 100, // user can add no more then 20 files at a time
        chunk_size : '1mb',
        unique_names : true,
        multiple_queues : true,
        rename: true,        
        sortable: true,        
        filters : [
            {title : "图片", extensions : "jpg,gif,png,jpeg,bmp"},
            {title : "归档", extensions : "zip,rar,7z,tar,gz,bz2"},
            {title : "办公", extensions : "doc,docx,txt,ppt,pptx,xls,xlsx,pdf,ini"},
            {title : "多媒体", extensions : "mp3,avi,mp4,flv,swf"},
            {title : "模板文件", extensions : "tpl"},
            {title : "PHP/JS/CSS", extensions : "php,js,css"}
        ],        
        flash_swf_url : '../../shared/plupload/plupload.flash.swf'
    });    
    $('#upload-from').submit(function(){
        showWaitMask('正在上传，这可能需要几分钟时间，请耐心等待...');
    });
    
    function createDirCallback(data){
        if (data.success) {
            alert('目录创建成功!');
            window.location.reload();
        } else {
            alert(data.msg);
            hideWaitMask();
        }
    }
    function uploadedCallback(data){
        if (data.success) {
            if(data.msg){
                alert(data.msg);
            }else{
                alert('文件上传完成.');
            }
            window.location.reload();
        }else{
            alert(data.msg);
        }
    }
});